import DeepCopy from './libs/deepcopy'

export function isType(o, typeStr) {
  return Object.prototype.toString.call(o).substr(8, typeStr.length) == typeStr
}

export const deepCopy = DeepCopy

export function inType(o, typesArr) {
  typesArr = typesArr || []
  for (let i = 0; i < typesArr.length; i++) {
    if (Object.prototype.toString.call(o).substr(8, typesArr[i].length) == typesArr[i]) {
      return true
    }
  }
  return false
}

export function isNull(o) {
  return isType(o, 'Null')
}
// 返回参数列表中第一个有定义的值
export function select() {
  const arr = Array.prototype.slice.call(arguments)
  for (const i in arr) {
    if (typeof arr[i] != 'undefined') {
      return arr[i]
    }
  }
  return undefined
}
// 判断对象中是否含有某属性
export function hasKey(o, key) {
  const keys = Object.keys(o || {})
  return keys.indexOf(key) > -1
}

export function isUndefined( v ){
  return isType( v, 'Undefined')
}

export function randomId(){
  const date = new Date()
  const t = String(date.getHours()) + String(date.getMinutes()) + String(date.getSeconds()) + String(date.getMilliseconds())
  const ramdom = String( Math.round(Math.random()*10000) )
  return t + '-' + ramdom
}

export function inBox(el , boxEl){
  const elpos = $(el).offset()
  const boxPos = $(boxEl).offset()
  boxPos.h = $(boxEl).height()
  boxPos.w = $(boxEl).width()
  return ( elpos.top >= boxPos.top && elpos.top <= boxPos.top + boxPos.h 
      && elpos.left >= boxPos.left && elpos.left <= boxPos.left + boxPos.w
    )
}

export function pickNumber( str = '' ){
  str = String(str)
  let re = str.replace(/[^0-9|^\.|^\-]{1,}/g, '')
  return re ? Number(re) : 0
}

export default {
  isType,
  inType,
  isNull,
  select,
  hasKey,
  isUndefined,
  randomId,
  pickNumber,
  deepCopy
}